<?php
namespace api\modules\v1\controllers;

use Yii;
use api\common\controllers\AController;
use common\records\Record;
use common\records\Order;
use common\records\ToyMachine;
use common\records\Toy;
use common\records\UserAchievement;
use common\records\Achievement;
use yii\data\ActiveDataProvider;

class RecordController extends AController
{
    public $modelClass = 'common\records\Record';
    /**
     * @api {get} /v1/records 记录列表
     * @apiVersion 1.0.0
     * @apiName index
     * @apiGroup records
     *
     * @apiParam {String} uid 用户id
     *
     */
    public function actionIndex()
    {
        if(!array_key_exists("uid",$_GET) )
        {
            $result = array();
            $detail = array();

            $detail['code'] = 400;
            $detail['reason'] = "fields missing";
            $result['result'] = $detail;
            return $result;
        }
        $uid = $_GET['uid'];
        $query = Record::find()->where([ 'uid'=>$uid]);
        return new ActiveDataProvider([
            'query' => $query,
            'pagination' => [
                'pageSize' => 10,
            ],
            'sort' => [
                'defaultOrder' => [
                    'id' => SORT_DESC
                ]
            ]
        ]);
    }

    /*
     @param order_id 订单id
     @param uid      用户ID
     @param status   抓娃娃结果
     */
    public function actionCreate()
    {
        $record = new $this->modelClass(); 
        if(!array_key_exists("order_id",$_POST) || !array_key_exists("uid",$_POST)|| !array_key_exists("status",$_POST) )
        {
            $result = array();
            $detail = array();

            $detail['code'] = 400;
            $detail['reason'] = "字段缺失";
            $result['result'] = $detail;
            return $result;

        }
        $order_id = intval(trim($_POST['order_id']));
        $uid = trim($_POST['uid']);
        $status = intval(trim($_POST['status']));
        $order_info = Order::find()->where(['order_id'=>$order_id, 'uid'=>$uid])->one();
        if($order_info == null)
        {
            $result = array();
            $detail = array();

            $detail['code'] = 400;
            $detail['reason'] = "用户不拥有该订单";
            $result['result'] = $detail;
            return $result;
        }

        $record->uid = $order_info->uid;
        $record->coin = $order_info->coin;
        $record->type = 2;
        $record->order_id= $order_id;
        $record->machine_id = $order_info->machine_id;

        $machine = ToyMachine::find()->where([ 'mid'=>$order_info->machine_id])->one();
        $record->timestamp = $order_info->timestamp;
        $record->status = $status;
        $record->toy_id = $machine->toy_id;

        $toy = Toy::find()->where(['id'=>$machine->toy_id])->one();
        $record->toy_name = $toy->name;
        $record->description = $machine->description;
        $record->save();
        if($status == 1)
        {
            $machine->left_toy = $machine->left_toy-1;
            $machine->save(false);
        }
        $order_info->status = 1;
        $order_info->save(false);


        $result = array();
        $detail = array();

        $detail['code'] = 200;
        $detail['reason'] = "同步结果成功";
        $result['result'] = $detail;
        
        return $result;
    }
}
